#! /usr/bin/python

# usage: ./dir2html start_directory python_logo.png

import os
import time
import md5
import sys

def gen_md5(f):
    fl = open(f, 'r')
    dig = md5.new(fl.read())
    fl.close();
    return dig.hexdigest()

def get_size(s):
    if s >= 1024*1024:
        return str(round(s/(1024.0*1024.0), 2)) + " MBytes"
    elif s >= 1024:
        return str(round(s/1024.0, 2)) + " KBytes"
    else:
        return str(s) + " Bytes"

start_dir = sys.argv[1]

for root, dirs, files in os.walk(start_dir):
	f = open(os.path.join(root,"index.html"), 'w')
	f.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">")
	f.write("<html>\n")

	f.write("<head>")
	f.write("</head>")

	f.write("<style type=\"text/css\">\n")
	f.write("<!--\n")
	f.write("table, th, td {\n")
	#f.write(" border:1px;\n")
	#f.write(" border-style:solid;\n")
	#f.write(" border-collapse:collapse;")
	f.write(" font-size:10pt;\n")
	f.write(" font-family:DejaVu,Helvetica,Arial,Sans-Serif;\n")
	f.write("}\n")
	f.write("p {\n")
	f.write(" font-size:10pt;\n")
	f.write(" font-family:DejaVu,Helvetica,Arial,Sans-Serif;\n")
	f.write("}\n")
	f.write("a:link { color:#000088; text-decoration:none; font-weight:normal; }")
	f.write("a:visited { color:#5555FF; text-decoration:none; font-weight:normal; }")
	f.write("a:hover { color:#0000AA; text-decoration:underline; font-weight:normal; }") # background-color:#EEEEEE;
	f.write("a:active { color:#0000EE; font-weight:normal; }") # background-color:#FFFF99;
	f.write("-->\n")
	f.write("</style>")
	f.write("</head>")

	f.write("<body>\n")
	f.write("<h1>" + root + "</h1>\n")

	try:
		df = open(os.path.join(root,"dir_desc.txt"))
		f.write(df.read())
		df.close()
	except:
		pass

	directories = [".", ".."]
	directories.extend(dirs)

	f.write("<hr/>\n")
	
	f.write("<table>\n")
	f.write("<tr>\n")
	f.write("<th>Directories</th>\n")
	f.write("</tr>\n")

	line = 0

	for di in directories:
		print di
		if line % 2 == 0:
			f.write("<tr bgcolor=\"#EEEEEE\">\n")
		else:
			f.write("<tr bgcolor=\"#E0E0E0\">\n")
		f.write("<td><a href=\"" + di + "/index.html\"><i>" + di + "</i></a></td>\n")
		f.write("</tr>\n")
		line = line + 1

	f.write("</table>\n")

	f.write("<table>\n")

	f.write("<tr>\n")
	f.write("<th>Files</th>\n")
	f.write("<th>MD5</th>\n") 
	f.write("<th>Size</th>\n")
	f.write("<th>Last modification time</th>\n")
	f.write("</tr>\n")

	for fi in files:
		if not (fi == "index.html" or fi == "dir_desc.txt"):
			print fi			
			st = os.stat(os.path.join(root,fi))
			if line % 2 == 0:
				f.write("<tr bgcolor=\"#EEEEEE\">\n")
			else:
			    f.write("<tr bgcolor=\"#E0E0E0\">\n")
			f.write("<td>")
			f.write("<a href=\"" + fi + "\">" + fi + "</a>")
			f.write("</td>\n")
			f.write("<td><code>  " + str(gen_md5(os.path.join(root,fi))) + "  </code></td>\n")
			f.write("<td>  " + get_size(st.st_size) + "  </td>\n")
			f.write("<td>" + str(time.ctime(st.st_mtime)) + "</td>\n")
			f.write("</tr>\n")
			line = line + 1

	f.write("</table>\n")
	f.write("<hr/>\n")

	f.write("<p><i>This file was automatically generated by <code>dir2html.py</code> on " + time.ctime(time.time()) + ". Copyright (c) 2007 by Christoph Schunk.</i></p>")

	f.write("<p> <img src=\"" + sys.argv[2] + "\" align=center alt=\"[Python Powered]\" border=0><a href=\"http://www.python.org/\">Python powered.</a></p>")
	
	f.write("</body>\n")

	f.write("</html>\n")

	f.close()
